/**
 * Created by JetBrains WebStorm.
 * User: Administrator
 * Date: 11-8-18
 * Time: 下午5:15
 * To change this template use File | Settings | File Templates.
 */


(function() {
    F = function() {
        F.eles = [];
        F.elesi = null;
    }
    window['F'] = F;

    F.browser = {
        userAgent: navigator.userAgent.toLowerCase(),
        ie:function(v) {
            return /msie/.test(this.userAgent) && !/opera/.test(this.userAgent);
        }
    };
    F.trim = function(str) {
        var space = ' ';
        while (str.charAt(0) === space) {
            str = str.substr(1);
        }
        while (str.charAt(str.length - 1) === space) {
            str = str.substr(0, str.length - 1);
        }
        return str;

    }
    F.eq = function(i) {
        F.elesi = F.makeArray(F.eles[i]);
        return this;
    }
    F.get = function(i) {
        if (i != undefined)
            return F.eles[i];
        return F.eles;
    }
    F.find = function(selector, context) {

    }
    F.inArray = function(elem, array) {
        for (var i = 0, iLen = array.length; i < iLen; i++)
            if (array[ i ] === elem)
                return i;

        return false;
    },
    F.mergeArray = function(a, v, act) {
            var t = [];
            switch (act) {
                case '-':
                {//remove

                    for (var i = 0,iLen = a.length; i < iLen; i++) {
                        if (F.inArray(a[i], v) === false) {
                            t.push(a[i]);
                        }
                    }
                    break;
                }
                case 'x':
                {//cross
                    for (var j = 0,jLen = v.length; j < jLen; j++) {
                        if (F.inArray(v[j], a) !== false) {
                            t.push(v[j]);
                        }
                    }
                    break;
                }
                case '+':
                {//merge
                    for (var j = 0,jLen = v.length; j < jLen; j++) {
                        if (F.inArray(v[j], a) === false) {
                            a.push(v[j]);
                        }
                    }
                    t = a;
                    break;
                }
                default:
                {
                    for (var j = 0,jLen = v.length; j < jLen; j++) {
                        a.push(v[j]);
                    }
                    t = a;
                    break;
                }

            }


            return t;
    }
    F.makeArray = function(array) {
        var ret = [];
        if (array != null) {
            var i = array.length;
            //单个元素，但是window, strings、 functions也有 'length'的属性，那么加其它的判断 
            if (i == null || array.split || array.setInterval || array.call)
                ret[0] = array;
            else//类数组的集合
                while (i)
                    ret[--i] = array[i];//Clone数组
        }

        return ret;
    }
    F.nodeName = function(elem, name) {
        return elem.nodeName &&
            elem.nodeName.toUpperCase() == name.toUpperCase();
    },
    F.getType = function(o) {
        
            var _t;
            return ((_t = typeof(o)) == "object" ?
                o == null && "null" || Object.prototype.toString.call(o).slice(8, -1) :
                _t
                ).toLowerCase();
    }
    F.addClass = function(name, nodes) {
        var nodes = nodes || F.elesi || F.eles;
        if (nodes.length) {
            for (var i = 0,iLen = nodes.length; i < iLen; i++) {
                var oClass = nodes[i].className,name = F.trim(name);
                nodes[i].className = (F.mergeArray(oClass.split(/\s+/),
                    (name).split(/\s+/),
                    '+'
                )
                    ).join(' ');
            }
        }
        
        return this;
    }
    F.removeClass = function(name, nodes) {
        var nodes = nodes || F.elesi || F.eles;
        if (nodes.length) {
            for (var i = 0,iLen = nodes.length; i < iLen; i++) {
                var oClass = F.trim(nodes[i].className),name = F.trim(name);
                nodes[i].className = (F.mergeArray(oClass.split(/\s+/),
                    (name).split(/\s+/),
                    '-'
                )
                    ).join(' ');
            }
        }
        return this;
    }
    F.css = function(option, nodes) {

        return this;
    }
    F.getAttr = function(name, nodes) {
        var v = [],nodes = nodes || F.elesi || F.eles;
        if (nodes.length) {
            for (var i = 0,iLen = nodes.length; i < iLen; i++) {
                {
                    var elem = nodes[i];
                    if (F.nodeName(elem, "form") && elem.getAttributeNode(name)) {
                        v.push(elem.getAttributeNode(name).nodeValue);
                        continue;
                    }
                    if (name == "style" && F.browser.ie()) {
                        v.push(elem.style['cssText']);
                        continue;
                    }
                    v.push(elem.getAttribute(name, 2) || elem.getAttribute(name));
                }
            }
        }
        return v;
    }
    F.setAttr = function(option, nodes) {
        var nodes = nodes || F.elesi || F.eles;
        if (nodes.length) {
            for (var i = 0,iLen = nodes.length; i < iLen; i++) {
                {
                    var elem = nodes[i];
                    for (var j in option) {
                        elem.setAttribute(j, option[j]);
                    }
                }
            }
        }
        return this;
    }
    F.getEle = function(selector, context) {
        if (typeof selector != "string") return;
        var selector = this.trim(selector);
        var selectorArr = selector.split(/\s+/),
            ele = [],last = [],
            context = F.makeArray(context || document);
        var get = {
            parse:/[#\.]|\w+/g,
            byId:function(id, con) {
                var id = id.substr(1);
                return con.getElementById(id);
            },
            byTag:function(tag, con) {
                return con.getElementsByTagName(tag);
            },
            byClass:function(str, con) {
                var strArr = str.split('.');
                var tmpele = con.getElementsByTagName(strArr[0] ? strArr[0] : '*');
                var tmp = [];
                for (var i = 0,iLen = tmpele.length; i < iLen; i++) {
                    var tmpCName = tmpele[i].className.split(/\s+/);
                    for (var j = 0,jLen = tmpCName.length; j < jLen; j++) {
                        if (tmpCName[j] == strArr[1]) {
                            tmp.push(tmpele[i]);
                        }
                    }
                }
                return tmp;
            },
            by:function(str, context) {
                var parseArr = str.match(this.parse);
                var contextArr = [];
                if (parseArr[0] == '#') {
                    contextArr = get.byId(str, context);
                } else if (parseArr.length == 1) {
                    contextArr = get.byTag(str, context);
                } else if (parseArr[0] == '.' || parseArr[1] == '.') {
                    contextArr = get.byClass(str, context);
                }
                return F.makeArray(contextArr);
            }
        }
        for (var i = 0,iLen = selectorArr.length; i < iLen; i++) {
            for (var j = 0,jLen = context.length; j < jLen; j++) {
                last = last.concat(get.by(selectorArr[i], context[j]));
            }
            ele = context = last;
            last = [];
        }
        F.eles = ele;
        return this;
    }


})();
window.onload = function() {


    alert(F.getEle('a').getAttr('href'));

}
